********************************************************************************
** 	TITLE:		CA06_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       CA06_results                                                  **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "CA06_results.smcl", replace

* Download the "table_tableau12.csv" file from this link: https://www.elections.ca/scripts/OVR2006/25/data_donnees/table_tableau12.csv and rename "table_tableau12.csv" to "CA06_results.csv"

* Import the dataset

import delimited "CA06_results.csv", delimiter(`"","') bindquote(nobind) clear

* Rename and (re)create variables

drop province
rename electoraldistrictcirconscription province
drop candidatecandidat
drop candidateresidencerésidenceducan
rename candidateoccupationprofessionduc districtname06
drop votesobtainedvotesobtenus
drop percentageofvotesobtainedpourcen
drop majoritymajorité
drop majoritypercentagepourcentagedem
rename v10 candidate06
drop v11 v12 v13 v14
gen residence06 = v15 + "," + v16
drop v15 v16
drop v17 v18 v19 v20
gen occupation06 = v21 + v22
drop v21 v22 v23
rename v24 numberofvotes06
rename v25 numberofvotespercent06
rename v26 majoritynumber06
rename v27 majoritypercent06
drop v28 v29 v30

* Generate dichotomous variable for each party

gen lib06 = strpos(candidate06, "Liberal/Libéral") > 0
gen con06 = strpos(candidate06, "Conservative/Conservateur") > 0
gen ndp06 = strpos(candidate06, "N.D.P./N.P.D.") > 0
gen green06 = strpos(candidate06, "Green Party/Parti vert") > 0
gen bq06 = strpos(candidate06, "Bloc Québécois/Bloc Québécois") > 0

* Generate party variable

gen party06 = 1 if lib06 == 1
replace party06 = 2 if con06 == 1
replace party06 = 3 if ndp06 == 1
replace party06 = 4 if bq06 == 1
replace party06 = 5 if green06 == 1
replace party06 = 88 if party06 == . 

* Generate rank of parties

gsort districtname06 -numberofvotes06
bysort districtname06 : gen rank06 = _n

gen first06 = party06 if rank06 == 1
gen second06 = party06 if rank06 == 2
gen third06 = party06 if rank06 == 3

bysort districtname06 : gen winner06 = first06[1]
bysort districtname06 : gen runnerup06 = second06[2]
bysort districtname06 : gen thirdplace06 = third06[3]

* Generate margins of victory

bysort districtname06 : gen margin06 = numberofvotespercent06[1] - numberofvotespercent06[2]

* Generate effective number of electoral parties

gen sqrvotepr06 = (numberofvotespercent06/100)^2

bysort districtname06 : egen sumsqrvotepr06 = sum(sqrvotepr06)

gen enep06 = 1/sumsqrvotepr06

* Labelling parties

label define party 1 "Liberal Party" 2 "Conservative Party" 3 "New Democratic Party" 4 "Bloc Quebecois" 5 "Green Party" 88 "Other"
label values winner06 runnerup06 thirdplace06 party

* Generate district numbers

gen districtnumber06 = .

replace districtnumber06 = 10001 if districtname06 == "Avalon"
replace districtnumber06 = 10002 if districtname06 == "Bonavista--Gander--Grand Falls--Windsor"
replace districtnumber06 = 10003 if districtname06 == "Humber--St. Barbe--Baie Verte"
replace districtnumber06 = 10004 if districtname06 == "Labrador"
replace districtnumber06 = 10005 if districtname06 == "Random--Burin--St. George's"
replace districtnumber06 = 10006 if districtname06 == "St. John's East/St. John's-Est"
replace districtnumber06 = 10007 if districtname06 == "St. John's South--Mount Pearl/St. John's-Sud--Mount Pearl"
replace districtnumber06 = 11001 if districtname06 == "Cardigan"
replace districtnumber06 = 11002 if districtname06 == "Charlottetown"
replace districtnumber06 = 11003 if districtname06 == "Egmont"
replace districtnumber06 = 11004 if districtname06 == "Malpeque"
replace districtnumber06 = 12001 if districtname06 == "Cape Breton--Canso"
replace districtnumber06 = 12002 if districtname06 == "Central Nova/Nova-Centre"
replace districtnumber06 = 12003 if districtname06 == "Dartmouth--Cole Harbour"
replace districtnumber06 = 12004 if districtname06 == "Halifax"
replace districtnumber06 = 12005 if districtname06 == "Halifax West/Halifax-Ouest"
replace districtnumber06 = 12006 if districtname06 == "Kings--Hants"
replace districtnumber06 = 12007 if districtname06 == "Cumberland--Colchester--Musquodoboit Valley"
replace districtnumber06 = 12008 if districtname06 == "Sackville--Eastern Shore"
replace districtnumber06 = 12009 if districtname06 == "South Shore--St. Margaret's"
replace districtnumber06 = 12010 if districtname06 == "Sydney--Victoria"
replace districtnumber06 = 12011 if districtname06 == "West Nova/Nova-Ouest"
replace districtnumber06 = 13001 if districtname06 == "Acadie--Bathurst"
replace districtnumber06 = 13002 if districtname06 == "Beauséjour"
replace districtnumber06 = 13003 if districtname06 == "Fredericton"
replace districtnumber06 = 13004 if districtname06 == "Fundy Royal"
replace districtnumber06 = 13005 if districtname06 == "Madawaska--Restigouche"
replace districtnumber06 = 13006 if districtname06 == "Miramichi"
replace districtnumber06 = 13007 if districtname06 == "Moncton--Riverview--Dieppe"
replace districtnumber06 = 13008 if districtname06 == "New Brunswick Southwest/Nouveau-Brunswick-Sud-Ouest"
replace districtnumber06 = 13009 if districtname06 == "Saint John"
replace districtnumber06 = 13010 if districtname06 == "Tobique--Mactaquac"
replace districtnumber06 = 24001 if districtname06 == "Abitibi--Témiscamingue"
replace districtnumber06 = 24002 if districtname06 == "Ahuntsic"
replace districtnumber06 = 24003 if districtname06 == "Alfred-Pellan"
replace districtnumber06 = 24004 if districtname06 == "Argenteuil--Papineau--Mirabel"
replace districtnumber06 = 24005 if districtname06 == "Beauce"
replace districtnumber06 = 24006 if districtname06 == "Beauharnois--Salaberry"
replace districtnumber06 = 24007 if districtname06 == "Beauport--Limoilou"
replace districtnumber06 = 24008 if districtname06 == "Berthier--Maskinongé"
replace districtnumber06 = 24009 if districtname06 == "Bourassa"
replace districtnumber06 = 24010 if districtname06 == "Brome--Missisquoi"
replace districtnumber06 = 24011 if districtname06 == "Brossard--La Prairie"
replace districtnumber06 = 24012 if districtname06 == "Chambly--Borduas"
replace districtnumber06 = 24013 if districtname06 == "Charlesbourg--Haute-Saint-Charles"
replace districtnumber06 = 24014 if districtname06 == "Montmorency--Charlevoix--Haute-Côte-Nord"
replace districtnumber06 = 24015 if districtname06 == "Châteauguay--Saint-Constant"
replace districtnumber06 = 24016 if districtname06 == "Chicoutimi--Le Fjord"
replace districtnumber06 = 24017 if districtname06 == "Compton--Stanstead"
replace districtnumber06 = 24018 if districtname06 == "Drummond"
replace districtnumber06 = 24019 if districtname06 == "Gaspésie--Îles-de-la-Madeleine"
replace districtnumber06 = 24020 if districtname06 == "Gatineau"
replace districtnumber06 = 24021 if districtname06 == "Hochelaga"
replace districtnumber06 = 24022 if districtname06 == "Honoré-Mercier"
replace districtnumber06 = 24023 if districtname06 == "Hull--Aylmer"
replace districtnumber06 = 24024 if districtname06 == "Jeanne-Le Ber"
replace districtnumber06 = 24025 if districtname06 == "Joliette"
replace districtnumber06 = 24026 if districtname06 == "Jonquière--Alma"
replace districtnumber06 = 24027 if districtname06 == "Lac-Saint-Louis"
replace districtnumber06 = 24028 if districtname06 == "La Pointe-de-l'Île"
replace districtnumber06 = 24029 if districtname06 == "LaSalle--Émard"
replace districtnumber06 = 24030 if districtname06 == "Laurentides--Labelle"
replace districtnumber06 = 24031 if districtname06 == "Laurier--Sainte-Marie"
replace districtnumber06 = 24032 if districtname06 == "Laval"
replace districtnumber06 = 24033 if districtname06 == "Laval--Les Îles"
replace districtnumber06 = 24034 if districtname06 == "Lévis--Bellechasse"
replace districtnumber06 = 24035 if districtname06 == "Longueuil--Pierre-Boucher"
replace districtnumber06 = 24036 if districtname06 == "Lotbinière--Chutes-de-la-Chaudière"
replace districtnumber06 = 24037 if districtname06 == "Louis-Hébert"
replace districtnumber06 = 24038 if districtname06 == "Louis-Saint-Laurent"
replace districtnumber06 = 24039 if districtname06 == "Manicouagan"
replace districtnumber06 = 24040 if districtname06 == "Marc-Aurèle-Fortin"
replace districtnumber06 = 24041 if districtname06 == "Haute-Gaspésie--La Mitis--Matane--Matapédia"
replace districtnumber06 = 24042 if districtname06 == "Mégantic--L'Érable"
replace districtnumber06 = 24043 if districtname06 == "Montcalm"
replace districtnumber06 = 24044 if districtname06 == "Mount Royal/Mont-Royal"
replace districtnumber06 = 24045 if districtname06 == "Notre-Dame-de-Grâce--Lachine"
replace districtnumber06 = 24046 if districtname06 == "Abitibi--Baie-James--Nunavik--Eeyou"
replace districtnumber06 = 24047 if districtname06 == "Outremont"
replace districtnumber06 = 24048 if districtname06 == "Papineau"
replace districtnumber06 = 24049 if districtname06 == "Pierrefonds--Dollard"
replace districtnumber06 = 24050 if districtname06 == "Pontiac"
replace districtnumber06 = 24051 if districtname06 == "Portneuf--Jacques-Cartier"
replace districtnumber06 = 24052 if districtname06 == "Québec"
replace districtnumber06 = 24053 if districtname06 == "Repentigny"
replace districtnumber06 = 24054 if districtname06 == "Bas-Richelieu--Nicolet--Bécancour"
replace districtnumber06 = 24055 if districtname06 == "Richmond--Arthabaska"
replace districtnumber06 = 24056 if districtname06 == "Rimouski-Neigette--Témiscouata--Les Basques"
replace districtnumber06 = 24057 if districtname06 == "Rivière-des-Mille-Îles"
replace districtnumber06 = 24058 if districtname06 == "Montmagny--L'Islet--Kamouraska--Rivière-du-Loup"
replace districtnumber06 = 24059 if districtname06 == "Rivière-du-Nord"
replace districtnumber06 = 24060 if districtname06 == "Roberval--Lac-Saint-Jean"
replace districtnumber06 = 24061 if districtname06 == "Rosemont--La Petite-Patrie"
replace districtnumber06 = 24062 if districtname06 == "Saint-Bruno--Saint-Hubert"
replace districtnumber06 = 24063 if districtname06 == "Saint-Hyacinthe--Bagot"
replace districtnumber06 = 24064 if districtname06 == "Saint-Jean"
replace districtnumber06 = 24065 if districtname06 == "Saint-Lambert"
replace districtnumber06 = 24066 if districtname06 == "Saint-Laurent--Cartierville"
replace districtnumber06 = 24067 if districtname06 == "Saint-Léonard--Saint-Michel"
replace districtnumber06 = 24068 if districtname06 == "Saint-Maurice--Champlain"
replace districtnumber06 = 24069 if districtname06 == "Shefford"
replace districtnumber06 = 24070 if districtname06 == "Sherbrooke"
replace districtnumber06 = 24071 if districtname06 == "Terrebonne--Blainville"
replace districtnumber06 = 24072 if districtname06 == "Trois-Rivières"
replace districtnumber06 = 24073 if districtname06 == "Vaudreuil-Soulanges"
replace districtnumber06 = 24074 if districtname06 == "Verchères--Les Patriotes"
replace districtnumber06 = 24075 if districtname06 == "Westmount--Ville-Marie"
replace districtnumber06 = 35001 if districtname06 == "Ajax--Pickering"
replace districtnumber06 = 35002 if districtname06 == "Algoma--Manitoulin--Kapuskasing"
replace districtnumber06 = 35003 if districtname06 == "Ancaster--Dundas--Flamborough--Westdale"
replace districtnumber06 = 35004 if districtname06 == "Barrie"
replace districtnumber06 = 35005 if districtname06 == "Beaches--East York"
replace districtnumber06 = 35006 if districtname06 == "Bramalea--Gore--Malton"
replace districtnumber06 = 35007 if districtname06 == "Brampton--Springdale"
replace districtnumber06 = 35008 if districtname06 == "Brampton West/Brampton-Ouest"
replace districtnumber06 = 35009 if districtname06 == "Brant"
replace districtnumber06 = 35010 if districtname06 == "Burlington"
replace districtnumber06 = 35011 if districtname06 == "Cambridge"
replace districtnumber06 = 35012 if districtname06 == "Carleton--Mississippi Mills"
replace districtnumber06 = 35013 if districtname06 == "Chatham-Kent--Essex"
replace districtnumber06 = 35014 if districtname06 == "Durham"
replace districtnumber06 = 35015 if districtname06 == "Davenport"
replace districtnumber06 = 35016 if districtname06 == "Don Valley East/Don Valley-Est"
replace districtnumber06 = 35017 if districtname06 == "Don Valley West/Don Valley-Ouest"
replace districtnumber06 = 35018 if districtname06 == "Dufferin--Caledon"
replace districtnumber06 = 35019 if districtname06 == "Eglinton--Lawrence"
replace districtnumber06 = 35020 if districtname06 == "Elgin--Middlesex--London"
replace districtnumber06 = 35021 if districtname06 == "Essex"
replace districtnumber06 = 35022 if districtname06 == "Etobicoke Centre/Etobicoke-Centre"
replace districtnumber06 = 35023 if districtname06 == "Etobicoke--Lakeshore"
replace districtnumber06 = 35024 if districtname06 == "Etobicoke North/Etobicoke-Nord"
replace districtnumber06 = 35025 if districtname06 == "Glengarry--Prescott--Russell"
replace districtnumber06 = 35026 if districtname06 == "Bruce--Grey--Owen Sound"
replace districtnumber06 = 35027 if districtname06 == "Guelph"
replace districtnumber06 = 35028 if districtname06 == "Haldimand--Norfolk"
replace districtnumber06 = 35029 if districtname06 == "Haliburton--Kawartha Lakes--Brock"
replace districtnumber06 = 35030 if districtname06 == "Halton"
replace districtnumber06 = 35031 if districtname06 == "Hamilton Centre/Hamilton-Centre"
replace districtnumber06 = 35032 if districtname06 == "Hamilton East--Stoney Creek/Hamilton-Est--Stoney Creek"
replace districtnumber06 = 35033 if districtname06 == "Hamilton Mountain"
replace districtnumber06 = 35034 if districtname06 == "Huron--Bruce"
replace districtnumber06 = 35035 if districtname06 == "Kenora"
replace districtnumber06 = 35036 if districtname06 == "Kingston and the Islands/Kingston et les Îles"
replace districtnumber06 = 35037 if districtname06 == "Kitchener Centre/Kitchener-Centre"
replace districtnumber06 = 35038 if districtname06 == "Kitchener--Conestoga"
replace districtnumber06 = 35039 if districtname06 == "Kitchener--Waterloo"
replace districtnumber06 = 35040 if districtname06 == "Lanark--Frontenac--Lennox and Addington"
replace districtnumber06 = 35041 if districtname06 == "Leeds--Grenville"
replace districtnumber06 = 35042 if districtname06 == "London--Fanshawe"
replace districtnumber06 = 35043 if districtname06 == "London North Centre/London-Centre-Nord"
replace districtnumber06 = 35044 if districtname06 == "London West/London-Ouest"
replace districtnumber06 = 35045 if districtname06 == "Markham--Unionville"
replace districtnumber06 = 35046 if districtname06 == "Lambton--Kent--Middlesex"
replace districtnumber06 = 35047 if districtname06 == "Mississauga--Brampton South/Mississauga--Brampton-Sud"
replace districtnumber06 = 35048 if districtname06 == "Mississauga East--Cooksville/Mississauga-Est--Cooksville"
replace districtnumber06 = 35049 if districtname06 == "Mississauga--Erindale"
replace districtnumber06 = 35050 if districtname06 == "Mississauga South/Mississauga-Sud"
replace districtnumber06 = 35051 if districtname06 == "Mississauga--Streetsville"
replace districtnumber06 = 35052 if districtname06 == "Nepean--Carleton"
replace districtnumber06 = 35053 if districtname06 == "Newmarket--Aurora"
replace districtnumber06 = 35054 if districtname06 == "Niagara Falls"
replace districtnumber06 = 35055 if districtname06 == "Niagara West--Glanbrook/Niagara-Ouest--Glanbrook"
replace districtnumber06 = 35056 if districtname06 == "Nickel Belt"
replace districtnumber06 = 35057 if districtname06 == "Nipissing--Timiskaming"
replace districtnumber06 = 35058 if districtname06 == "Northumberland--Quinte West"
replace districtnumber06 = 35059 if districtname06 == "Oak Ridges--Markham"
replace districtnumber06 = 35060 if districtname06 == "Oakville"
replace districtnumber06 = 35061 if districtname06 == "Oshawa"
replace districtnumber06 = 35062 if districtname06 == "Ottawa Centre/Ottawa-Centre"
replace districtnumber06 = 35063 if districtname06 == "Ottawa--Orléans"
replace districtnumber06 = 35064 if districtname06 == "Ottawa South/Ottawa-Sud"
replace districtnumber06 = 35065 if districtname06 == "Ottawa--Vanier"
replace districtnumber06 = 35066 if districtname06 == "Ottawa West--Nepean/Ottawa-Ouest--Nepean"
replace districtnumber06 = 35067 if districtname06 == "Oxford"
replace districtnumber06 = 35068 if districtname06 == "Parkdale--High Park"
replace districtnumber06 = 35069 if districtname06 == "Parry Sound--Muskoka"
replace districtnumber06 = 35070 if districtname06 == "Perth--Wellington"
replace districtnumber06 = 35071 if districtname06 == "Peterborough"
replace districtnumber06 = 35072 if districtname06 == "Pickering--Scarborough East/Pickering--Scarborough-Est"
replace districtnumber06 = 35073 if districtname06 == "Prince Edward--Hastings"
replace districtnumber06 = 35074 if districtname06 == "Renfrew--Nipissing--Pembroke"
replace districtnumber06 = 35075 if districtname06 == "Richmond Hill"
replace districtnumber06 = 35076 if districtname06 == "St. Catharines"
replace districtnumber06 = 35077 if districtname06 == "St. Paul's"
replace districtnumber06 = 35078 if districtname06 == "Sarnia--Lambton"
replace districtnumber06 = 35079 if districtname06 == "Sault Ste. Marie"
replace districtnumber06 = 35080 if districtname06 == "Scarborough--Agincourt"
replace districtnumber06 = 35081 if districtname06 == "Scarborough Centre/Scarborough-Centre"
replace districtnumber06 = 35082 if districtname06 == "Scarborough--Guildwood"
replace districtnumber06 = 35083 if districtname06 == "Scarborough--Rouge River"
replace districtnumber06 = 35084 if districtname06 == "Scarborough Southwest/Scarborough-Sud-Ouest"
replace districtnumber06 = 35085 if districtname06 == "Simcoe--Grey"
replace districtnumber06 = 35086 if districtname06 == "Simcoe North/Simcoe-Nord"
replace districtnumber06 = 35087 if districtname06 == "Stormont--Dundas--South Glengarry"
replace districtnumber06 = 35088 if districtname06 == "Sudbury"
replace districtnumber06 = 35089 if districtname06 == "Thornhill"
replace districtnumber06 = 35090 if districtname06 == "Thunder Bay--Rainy River"
replace districtnumber06 = 35091 if districtname06 == "Thunder Bay--Superior North/Thunder Bay--Superior-Nord"
replace districtnumber06 = 35092 if districtname06 == "Timmins--James Bay/Timmins--Baie James"
replace districtnumber06 = 35093 if districtname06 == "Toronto Centre/Toronto-Centre"
replace districtnumber06 = 35094 if districtname06 == "Toronto--Danforth"
replace districtnumber06 = 35095 if districtname06 == "Trinity--Spadina"
replace districtnumber06 = 35096 if districtname06 == "Vaughan"
replace districtnumber06 = 35097 if districtname06 == "Welland"
replace districtnumber06 = 35098 if districtname06 == "Wellington--Halton Hills"
replace districtnumber06 = 35099 if districtname06 == "Whitby--Oshawa"
replace districtnumber06 = 35100 if districtname06 == "Willowdale"
replace districtnumber06 = 35101 if districtname06 == "Windsor--Tecumseh"
replace districtnumber06 = 35102 if districtname06 == "Windsor West/Windsor-Ouest"
replace districtnumber06 = 35103 if districtname06 == "York Centre/York-Centre"
replace districtnumber06 = 35104 if districtname06 == "York--Simcoe"
replace districtnumber06 = 35105 if districtname06 == "York South--Weston/York-Sud--Weston"
replace districtnumber06 = 35106 if districtname06 == "York West/York-Ouest"
replace districtnumber06 = 46001 if districtname06 == "Brandon--Souris"
replace districtnumber06 = 46002 if districtname06 == "Charleswood--St. James--Assiniboia"
replace districtnumber06 = 46003 if districtname06 == "Churchill"
replace districtnumber06 = 46004 if districtname06 == "Dauphin--Swan River--Marquette"
replace districtnumber06 = 46005 if districtname06 == "Elmwood--Transcona"
replace districtnumber06 = 46006 if districtname06 == "Kildonan--St. Paul"
replace districtnumber06 = 46007 if districtname06 == "Portage--Lisgar"
replace districtnumber06 = 46008 if districtname06 == "Provencher"
replace districtnumber06 = 46009 if districtname06 == "Saint Boniface/Saint-Boniface"
replace districtnumber06 = 46010 if districtname06 == "Selkirk--Interlake"
replace districtnumber06 = 46011 if districtname06 == "Winnipeg Centre/Winnipeg-Centre"
replace districtnumber06 = 46012 if districtname06 == "Winnipeg North/Winnipeg-Nord"
replace districtnumber06 = 46013 if districtname06 == "Winnipeg South/Winnipeg-Sud"
replace districtnumber06 = 46014 if districtname06 == "Winnipeg South Centre/Winnipeg-Centre-Sud"
replace districtnumber06 = 47001 if districtname06 == "Battlefords--Lloydminster"
replace districtnumber06 = 47002 if districtname06 == "Blackstrap"
replace districtnumber06 = 47003 if districtname06 == "Desnethé--Missinippi--Churchill River/Desnethé--Missinippi--Rivière Churchill"
replace districtnumber06 = 47004 if districtname06 == "Cypress Hills--Grasslands"
replace districtnumber06 = 47005 if districtname06 == "Palliser"
replace districtnumber06 = 47006 if districtname06 == "Prince Albert"
replace districtnumber06 = 47007 if districtname06 == "Regina--Lumsden--Lake Centre"
replace districtnumber06 = 47008 if districtname06 == "Regina--Qu'Appelle"
replace districtnumber06 = 47009 if districtname06 == "Saskatoon--Humboldt"
replace districtnumber06 = 47010 if districtname06 == "Saskatoon--Rosetown--Biggar"
replace districtnumber06 = 47011 if districtname06 == "Saskatoon--Wanuskewin"
replace districtnumber06 = 47012 if districtname06 == "Souris--Moose Mountain"
replace districtnumber06 = 47013 if districtname06 == "Wascana"
replace districtnumber06 = 47014 if districtname06 == "Yorkton--Melville"
replace districtnumber06 = 48001 if districtname06 == "Fort McMurray--Athabasca"
replace districtnumber06 = 48002 if districtname06 == "Calgary East/Calgary-Est"
replace districtnumber06 = 48003 if districtname06 == "Calgary Centre-North/Calgary-Centre-Nord"
replace districtnumber06 = 48004 if districtname06 == "Calgary Northeast/Calgary-Nord-Est"
replace districtnumber06 = 48005 if districtname06 == "Calgary--Nose Hill"
replace districtnumber06 = 48006 if districtname06 == "Calgary Centre/Calgary-Centre"
replace districtnumber06 = 48007 if districtname06 == "Calgary Southeast/Calgary-Sud-Est"
replace districtnumber06 = 48008 if districtname06 == "Calgary Southwest/Calgary-Sud-Ouest"
replace districtnumber06 = 48009 if districtname06 == "Calgary West/Calgary-Ouest"
replace districtnumber06 = 48010 if districtname06 == "Crowfoot"
replace districtnumber06 = 48011 if districtname06 == "Edmonton--Mill Woods--Beaumont"
replace districtnumber06 = 48012 if districtname06 == "Edmonton Centre/Edmonton-Centre"
replace districtnumber06 = 48013 if districtname06 == "Edmonton East/Edmonton-Est"
replace districtnumber06 = 48014 if districtname06 == "Edmonton--Leduc"
replace districtnumber06 = 48015 if districtname06 == "Edmonton--St. Albert"
replace districtnumber06 = 48016 if districtname06 == "Edmonton--Sherwood Park"
replace districtnumber06 = 48017 if districtname06 == "Edmonton--Spruce Grove"
replace districtnumber06 = 48018 if districtname06 == "Edmonton--Strathcona"
replace districtnumber06 = 48019 if districtname06 == "Lethbridge"
replace districtnumber06 = 48020 if districtname06 == "Macleod"
replace districtnumber06 = 48021 if districtname06 == "Medicine Hat"
replace districtnumber06 = 48022 if districtname06 == "Peace River"
replace districtnumber06 = 48023 if districtname06 == "Red Deer"
replace districtnumber06 = 48024 if districtname06 == "Vegreville--Wainwright"
replace districtnumber06 = 48025 if districtname06 == "Westlock--St. Paul"
replace districtnumber06 = 48026 if districtname06 == "Wetaskiwin"
replace districtnumber06 = 48027 if districtname06 == "Wild Rose"
replace districtnumber06 = 48028 if districtname06 == "Yellowhead"
replace districtnumber06 = 59001 if districtname06 == "Abbotsford"
replace districtnumber06 = 59002 if districtname06 == "Burnaby--Douglas"
replace districtnumber06 = 59003 if districtname06 == "Burnaby--New Westminster"
replace districtnumber06 = 59004 if districtname06 == "Cariboo--Prince George"
replace districtnumber06 = 59005 if districtname06 == "Chilliwack--Fraser Canyon"
replace districtnumber06 = 59006 if districtname06 == "Delta--Richmond East/Delta--Richmond-Est"
replace districtnumber06 = 59007 if districtname06 == "Pitt Meadows--Maple Ridge--Mission"
replace districtnumber06 = 59008 if districtname06 == "Esquimalt--Juan de Fuca"
replace districtnumber06 = 59009 if districtname06 == "Fleetwood--Port Kells"
replace districtnumber06 = 59010 if districtname06 == "Kamloops--Thompson--Cariboo"
replace districtnumber06 = 59011 if districtname06 == "Kelowna--Lake Country"
replace districtnumber06 = 59012 if districtname06 == "Kootenay--Columbia"
replace districtnumber06 = 59013 if districtname06 == "Langley"
replace districtnumber06 = 59014 if districtname06 == "Nanaimo--Alberni"
replace districtnumber06 = 59015 if districtname06 == "Nanaimo--Cowichan"
replace districtnumber06 = 59016 if districtname06 == "Newton--North Delta/Newton--Delta-Nord"
replace districtnumber06 = 59017 if districtname06 == "New Westminster--Coquitlam"
replace districtnumber06 = 59018 if districtname06 == "Okanagan--Shuswap"
replace districtnumber06 = 59019 if districtname06 == "North Vancouver"
replace districtnumber06 = 59020 if districtname06 == "Okanagan--Coquihalla"
replace districtnumber06 = 59021 if districtname06 == "Port Moody--Westwood--Port Coquitlam"
replace districtnumber06 = 59022 if districtname06 == "Prince George--Peace River"
replace districtnumber06 = 59023 if districtname06 == "Richmond"
replace districtnumber06 = 59024 if districtname06 == "Saanich--Gulf Islands"
replace districtnumber06 = 59025 if districtname06 == "Skeena--Bulkley Valley"
replace districtnumber06 = 59026 if districtname06 == "British Columbia Southern Interior/Colombie-Britannique-Southern Interior"
replace districtnumber06 = 59027 if districtname06 == "South Surrey--White Rock--Cloverdale/Surrey-Sud--White Rock--Cloverdale"
replace districtnumber06 = 59028 if districtname06 == "Surrey North/Surrey-Nord"
replace districtnumber06 = 59029 if districtname06 == "Vancouver Centre/Vancouver-Centre"
replace districtnumber06 = 59030 if districtname06 == "Vancouver East/Vancouver-Est"
replace districtnumber06 = 59031 if districtname06 == "Vancouver Island North/Île de Vancouver-Nord"
replace districtnumber06 = 59032 if districtname06 == "Vancouver Kingsway"
replace districtnumber06 = 59033 if districtname06 == "Vancouver Quadra"
replace districtnumber06 = 59034 if districtname06 == "Vancouver South/Vancouver-Sud"
replace districtnumber06 = 59035 if districtname06 == "Victoria"
replace districtnumber06 = 59036 if districtname06 == "West Vancouver--Sunshine Coast--Sea to Sky Country"
replace districtnumber06 = 60001 if districtname06 == "Yukon"
replace districtnumber06 = 61001 if districtname06 == "Western Arctic"
replace districtnumber06 = 62001 if districtname06 == "Nunavut"

* Selecting, ordering, and sorting variables

keep districtnumber06 districtname06 party06 rank06 numberofvotes06 numberofvotespercent06 margin06 enep06 winner06 runnerup06 thirdplace06

order districtnumber06 districtname06 party06 rank06 numberofvotes06 numberofvotespercent06 margin06 enep06 winner06 runnerup06 thirdplace06

sort districtnumber06 rank06

drop if rank06 > 3

* Drop duplicates

duplicates tag districtnumber06, gen(dup)
duplicates drop districtnumber06, force
drop dup

* Merge with CA04_results.dta

merge 1:1 _n using CA04_results.dta

rename _merge _merge04_06

* Generate variable for incumbent party reelection

gen reelected06 = 1 if winner06 == winner04
replace reelected06 = 0 if reelected06!=1

* Create election variable

gen election06 = "2006 Canadian federal election"

* Saving file

save CA0406_results, replace

log close